﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace JiuMeiMall.PMethod
{
    /// <summary>
    /// Pagination 的摘要说明。
    /// </summary>
    public class PaginationNew
    {

        /// <summary>
        /// 返回分页信息
        /// </summary>
        /// <param name="IndexPage">当前页</param>
        /// <param name="ReCount">记录总数</param>
        /// <param name="PageSize">一页显示N行</param>
        /// <param name="PageParameter">?后的字符串参数如：Buy_type=1＆keyword=中国</param>
        /// <param name="Att">返回中文或英文的分页信息</param>
        /// <returns></returns>
        public static string PageInfo(int IndexPage, int ReCount, int PageSize, string PageParameter, int Att)
        {
            return PageInfo(IndexPage, ReCount, PageSize, PageParameter, Att, true);
        }

        /// <summary>
        /// 返回分页信息
        /// </summary>
        /// <param name="IndexPage">当前页</param>
        /// <param name="ReCount">记录总数</param>
        /// <param name="PageSize">一页显示N行</param>
        /// <param name="PageParameter">?后的字符串参数如：Buy_type=1＆keyword=中国</param>
        /// <param name="Att">返回中文或英文的分页信息</param>
        /// <param name="showbutton">是否显示跳转按钮</param>
        /// <returns></returns>
        public static string PageInfo(int IndexPage, int ReCount, int PageSize, string PageParameter, int Att, bool showbutton)
        {
            string PageListTxt = "";
            if (Att == Config.si_CN)
            {
                PageListTxt = PageInfo_CN(IndexPage, ReCount, PageSize, showbutton);
            }
            else if (Att == Config.si_EN)
            {
                PageListTxt = PageInfo_EN(IndexPage, ReCount, PageSize, PageParameter, showbutton);
            }
            return PageListTxt;
        }

        /// <summary>
        /// 返回中文的分页信息
        /// </summary>
        /// <param name="IndexPage">当前页</param>
        /// <param name="ReCount">记录总数</param>
        /// <param name="PageSize">一页显示N行</param>
        /// <param name="PageParameter">?后的字符串参数如：Buy_type=1＆keyword=中国</param>
        /// <returns></returns>
        public static string PageInfo_CN(int IndexPage, int ReCount, int PageSize, string PageParameter)
        {
            return PageInfo_CN(IndexPage, ReCount, PageSize, true);
        }
        /// <summary>
        /// 返回中文的分页信息
        /// </summary>
        /// <param name="IndexPage">当前页</param>
        /// <param name="ReCount">记录总数</param>
        /// <param name="PageSize">一页显示N行</param>
        /// <param name="PageParameter">?后的字符串参数如：Buy_type=1＆keyword=中国</param>
        /// <param name="showbutton">是否显示跳转按钮</param>
        /// <returns></returns>
        public static string PageInfo_CN(int IndexPage, int ReCount, int PageSize, bool showbutton)
        {
            string PageListTxt = "";
            if (IndexPage <= 0)
            {
                IndexPage = 1;
            }
            int PageCount = Pagination.PageCount(ReCount, PageSize);
            if (IndexPage > PageCount)
            {
                IndexPage = PageCount;
            }
            int pageskip = 5;
            int Bpage = 1;
            int Epage = 1;
            if (PageCount >= pageskip && IndexPage > 4)
            {
                Bpage = IndexPage - 2;
                if (PageCount >= pageskip && Bpage > PageCount - (pageskip - 1)) Bpage = PageCount - (pageskip - 1);
            }
            Epage = Bpage + (pageskip - 1);
            if (Epage > PageCount) Epage = PageCount;


            PageListTxt = "<div class=\"pagin fr\" id=\"paginfr\">";
            //PageListTxt += "<span class=\"text\">共" + ReCount + "条记录</span>";
            //PageListTxt += "<span class=\"text\">共" + PageCount + "页</span>";
            PageListTxt += "<span class=\"text\">共" + ReCount + "条记录&nbsp;&nbsp;共" + PageCount + "页</span>";

            if (IndexPage > 1)
            {
                PageListTxt += "<span class=\"prev\" onclick=\"goPageList(" + (IndexPage - 1) + ",this);\">上一页</span>";
                PageListTxt += "<span class=\"prev\" onclick=\"goPageList(1,this);\">首页</span>";
            }
            else
            {
                PageListTxt += "<span class=\"prev-disabled\" onclick=\"goPageList(" + (IndexPage - 1) + ",this);\">上一页</span>";
                PageListTxt += "<span class=\"prev-disabled\" onclick=\"goPageList(1,this);\">首页</span>";
            }
            for (int i = Bpage; i <= Epage; i++)
            {
                if (i == IndexPage)
                {
                    PageListTxt += "<a href=\"javascript:goPageList(" + i + ");\" class=\"current\">" + i + "</a>";
                }
                else
                {
                    PageListTxt += "<a href=\"javascript:goPageList(" + i + ");\">" + i + "</a>";
                }
            }


            if (IndexPage < PageCount)
            {
                PageListTxt += "<span class=\"next\" onclick=\"goPageList(" + PageCount + ",this);\">末页</span>";
                PageListTxt += "<span class=\"next\" onclick=\"goPageList(" + (IndexPage + 1) + ",this);\">下一页<b></b></span>";
            }
            else
            {
                PageListTxt += "<span class=\"next-disabled\" onclick=\"goPageList(" + PageCount + ",this);\">末页</span>";
                PageListTxt += "<span class=\"next-disabled\" onclick=\"goPageList(" + (IndexPage + 1) + ",this);\">下一页<b></b></span>";
            }

            if (showbutton)
            {
                PageListTxt += "<em>转向第 <input name=\"pagerToPage\" type=\"text\" id=\"pagerToPage\" style=\"width:30px\" /> 页";
                PageListTxt += "<input value=\"确定\" onclick=\"goToPageList()\" type=\"button\" /></em>";
                PageListTxt += "</div>";
            }
            return PageListTxt;
        }

        /// <summary>
        /// 返回英文的分页信息
        /// </summary>
        /// <param name="IndexPage">当前页</param>
        /// <param name="ReCount">记录总数</param>
        /// <param name="PageSize">一页显示N行</param>
        /// <param name="PageParameter">?后的字符串参数如：Buy_type=1＆keyword=中国</param>
        /// <returns></returns>
        public static string PageInfo_EN(int IndexPage, int ReCount, int PageSize, string PageParameter)
        {
            return PageInfo_EN(IndexPage, ReCount, PageSize, PageParameter, true);
        }
        /// <summary>
        /// 返回英文的分页信息
        /// </summary>
        /// <param name="IndexPage">当前页</param>
        /// <param name="ReCount">记录总数</param>
        /// <param name="PageSize">一页显示N行</param>
        /// <param name="PageParameter">?后的字符串参数如：Buy_type=1＆keyword=中国</param>
        /// <param name="showbutton">是否显示跳转按钮</param>
        /// <returns></returns>
        public static string PageInfo_EN(int IndexPage, int ReCount, int PageSize, string PageParameter, bool showbutton)
        {
            string PageListTxt = "";
            int PageCount = Pagination.PageCount(ReCount, PageSize);
            int pageskip = 10;
            int Bpage = 1;
            int Epage = 1;
            if (PageCount >= pageskip && IndexPage > 6)
            {
                Bpage = IndexPage - 5;
                if (PageCount >= pageskip && Bpage > PageCount - (pageskip - 1)) Bpage = PageCount - (pageskip - 1);
            }
            Epage = Bpage + (pageskip - 1);
            if (Epage > PageCount) Epage = PageCount;


            PageListTxt = "<table border='0' cellpadding='0' style='border-collapse: collapse' width='100%' height='20'>";
            PageListTxt += "<tr>";
            PageListTxt += "<td align='right'>";
            PageListTxt += "" + ReCount + " records&nbsp;" + PageSize + "/page pages&nbsp;<b>" + IndexPage + "</b>/<b>" + PageCount + "</b>&nbsp;&nbsp;&nbsp;&nbsp;page:";

            if (IndexPage > 1)
            {
                PageListTxt += "<a href='?Page=1&" + PageParameter + "&RecordCount=" + ReCount + "' title='Page 1'><FONT face=webdings>9</FONT></a>";
                PageListTxt += "<a href='?Page=" + (IndexPage - 1) + "&" + PageParameter + "&RecordCount=" + ReCount + "' title='Prev'><FONT face=webdings>7</FONT></a>";
            }

            for (int i = Bpage; i <= Epage; i++)
            {
                if (i == IndexPage)
                    PageListTxt += " <b><font color='red'>" + i + "</font></b> ";
                else
                    PageListTxt += " <a href='?Page=" + i + "&" + PageParameter + "&RecordCount=" + ReCount + "' title='Page " + i + "'>" + i + "</a> ";
            }

            if (IndexPage < PageCount)
            {
                PageListTxt += "<a href='?Page=" + (IndexPage + 1) + "&" + PageParameter + "&RecordCount=" + ReCount + "' title='Next'><FONT face=webdings>8</FONT></a>";
                PageListTxt += "<a href='?Page=" + PageCount + "&" + PageParameter + "&RecordCount=" + ReCount + "' title='Page " + PageCount + "'><FONT face=webdings>:</FONT></a>";
            }

            PageListTxt += "&nbsp;</td>";
            if (showbutton)
            {
                PageListTxt += "<td><form style='line-height: 150%; margin-top: 0; margin-bottom: 0' name='Select_Page' action='?' mothed='get'><input type='text' name='Page' size='2'><input type='submit' value='Go' name='B1'>" + getPageParameterHidden(splitPageParameter(PageParameter)) + "<input type='hidden' name='RecordCount' value='" + ReCount + "'></form></td>";
            }
            PageListTxt += "</tr></table>";
            return PageListTxt;
        }

        /// <summary>
        /// 计算总页数
        /// </summary>
        /// <param name="ReCount">记录总数</param>
        /// <param name="PageSize">一页N行</param>
        /// <returns></returns>
        public static int PageCount(int ReCount, int PageSize)
        {
            return Convert.ToInt32(Math.Ceiling(ReCount / Convert.ToDouble(PageSize)));
        }

        /// <summary>
        /// 返回一个地址?后面的值
        /// 返回ArrayList，ArrayList[i]是一个字符串数组，PageListTxt[0]=名 PageListTxt[1]=值
        /// </summary>
        /// <param name="PageParameter">URL地址</param>
        /// <returns></returns>
        public static ArrayList splitPageParameter(string PageParameter)
        {
            ArrayList al = new ArrayList();
            if (PageParameter != null && PageParameter != "")
            {
                string[] pa = PMethod.StringOper.SplitMulti(PageParameter, "&");
                for (int i = 0; i < pa.Length; i++)
                {
                    if (pa[i] != null && pa[i] != "")
                    {
                        string[] UrlItem = PMethod.StringOper.SplitMulti(pa[i], "=");
                        if (UrlItem != null && UrlItem.Length >= 2)
                        {
                            al.Add(UrlItem);
                        }
                    }
                }
            }
            return al;
        }

        /// <summary>
        /// 将URL?后面的名/值生成＜input type='hidden'　name='名' value='值'＞的字符串
        /// </summary>
        /// <param name="al"></param>
        /// <returns></returns>
        public static string getPageParameterHidden(ArrayList al)
        {
            string txt = "";
            for (int i = 0; i < al.Count; i++)
            {
                try
                {
                    string[] UrlItem = (string[])al[i];
                    txt += "<input type='hidden' name='" + UrlItem[0] + "' value='" + UrlItem[1] + "'>";
                }
                catch { }
            }
            return txt;
        }

        /// <summary>
        /// 返回中文的分页信息
        /// </summary>
        /// <param name="IndexPage">当前页</param>
        /// <param name="ReCount">记录总数</param>
        /// <param name="PageSize">一页显示N行</param>
        /// <param name="PageParameter">?后的字符串参数如：Buy_type=1＆keyword=中国</param>
        /// <param name="showbutton">是否显示跳转按钮</param>
        /// <returns></returns>
        public static string PageInfo_CN_2(int IndexPage, int ReCount, int PageSize, bool showbutton)
        {
            string PageListTxt = "";
            if (IndexPage <= 0)
            {
                IndexPage = 1;
            }
            int PageCount = Pagination.PageCount(ReCount, PageSize);
            if (IndexPage > PageCount)
            {
                IndexPage = PageCount;
            }
            int pageskip = 5;
            int Bpage = 1;
            int Epage = 1;
            if (PageCount >= pageskip && IndexPage > 4)
            {
                Bpage = IndexPage - 2;
                if (PageCount >= pageskip && Bpage > PageCount - (pageskip - 1)) Bpage = PageCount - (pageskip - 1);
            }
            Epage = Bpage + (pageskip - 1);
            if (Epage > PageCount) Epage = PageCount;


            PageListTxt = "<div class=\"pagin fr\" id=\"paginfr2\">";
            PageListTxt += "<span class=\"text\">共" + ReCount + "条记录</span>";
            PageListTxt += "<span class=\"text\">共" + PageCount + "页</span>";
            if (IndexPage > 1)
            {
                PageListTxt += "<span class=\"prev\" onclick=\"goPageList2(" + (IndexPage - 1) + ",this);\">上一页</span>";
                PageListTxt += "<span class=\"prev\" onclick=\"goPageList2(1,this);\">首页</span>";
            }
            else
            {
                PageListTxt += "<span class=\"prev-disabled\" onclick=\"goPageList2(" + (IndexPage - 1) + ",this);\">上一页</span>";
                PageListTxt += "<span class=\"prev-disabled\" onclick=\"goPageList2(1,this);\">首页</span>";
            }
            for (int i = Bpage; i <= Epage; i++)
            {
                if (i == IndexPage)
                {
                    PageListTxt += "<a href=\"javascript:goPageList2(" + i + ");\" class=\"current\">" + i + "</a>";
                }
                else
                {
                    PageListTxt += "<a href=\"javascript:goPageList2(" + i + ");\">" + i + "</a>";
                }
            }


            if (IndexPage < PageCount)
            {
                PageListTxt += "<span class=\"next\" onclick=\"goPageList2(" + PageCount + ",this);\">末页</span>";
                PageListTxt += "<span class=\"next\" onclick=\"goPageList2(" + (IndexPage + 1) + ",this);\">下一页<b></b></span>";
            }
            else
            {
                PageListTxt += "<span class=\"next-disabled\" onclick=\"goPageList2(" + PageCount + ",this);\">末页</span>";
                PageListTxt += "<span class=\"next-disabled\" onclick=\"goPageList2(" + (IndexPage + 1) + ",this);\">下一页<b></b></span>";
            }

            if (showbutton)
            {
                PageListTxt += "<em>转向第 <input name=\"toPage\" size=\"2\" type=\"text\" id=\"pagerToPage2\" /> 页";
                PageListTxt += "<input value=\"确定\" onclick=\"goToPageList2()\" type=\"button\" /></em>";
                PageListTxt += "</div>";
            }
            return PageListTxt;
        }
        /// <summary>
        /// 返回中文的分页信息(样式不同用于MallWeb网站)
        /// </summary>
        /// <param name="IndexPage">当前页</param>
        /// <param name="ReCount">记录总数</param>
        /// <param name="PageSize">一页显示N行</param>
        /// <param name="PageParameter">?后的字符串参数如：Buy_type=1＆keyword=中国</param>
        /// <param name="showbutton">是否显示跳转按钮</param>
        /// <returns></returns>
        public static string PageInfo_CN_3(int IndexPage, int ReCount, int PageSize, bool showbutton)
        {
            string PageListTxt = "";
            if (IndexPage <= 0)
            {
                IndexPage = 1;
            }
            int PageCount = Pagination.PageCount(ReCount, PageSize);
            if (IndexPage > PageCount)
            {
                IndexPage = PageCount;
            }
            int pageskip = 5;
            int Bpage = 1;
            int Epage = 1;
            if (PageCount >= pageskip && IndexPage > 4)
            {
                Bpage = IndexPage - 2;
                if (PageCount >= pageskip && Bpage > PageCount - (pageskip - 1)) Bpage = PageCount - (pageskip - 1);
            }
            Epage = Bpage + (pageskip - 1);
            if (Epage > PageCount) Epage = PageCount;


            PageListTxt = "<div class=\"clearfix\"><p style=\"background:none;float:right;\" class=\"net_apage\">";
            PageListTxt += "<span>共" + ReCount + "条记录</span>";
            PageListTxt += "<span class=\"amount\">共" + PageCount + "页</span>";
            if (IndexPage > 1)
            {
                PageListTxt += "<a href=\"javascript:goPageList(" + (IndexPage - 1) + ",this);\">上一页</a>";
                PageListTxt += "<a href=\"javascript:goPageList(1,this);\">首页</a>";
            }
            else
            {
                PageListTxt += "<a class=\"prev-disabled\" href=\"javascript:goPageList(" + (IndexPage - 1) + ",this);\">上一页</a>";
                PageListTxt += "<a class=\"prev-disabled\" href=\"javascript:goPageList(1,this);\">首页</a>";
            }
            for (int i = Bpage; i <= Epage; i++)
            {
                if (i == IndexPage)
                {
                    PageListTxt += "<a href=\"javascript:goPageList(" + i + ");\" class=\"active\">" + i + "</a>";
                }
                else
                {
                    PageListTxt += "<a href=\"javascript:goPageList(" + i + ");\">" + i + "</a>";
                }
            }


            if (IndexPage < PageCount)
            {
                PageListTxt += "<a href=\"javascript:goPageList(" + PageCount + ",this);\">末页</a>";
                PageListTxt += "<a href=\"javascript:goPageList(" + (IndexPage + 1) + ",this);\">下一页</a>";
            }
            else
            {
                PageListTxt += "<a class=\"next-disabled\" href=\"javascript:goPageList(" + PageCount + ",this);\">末页</a>";
                PageListTxt += "<a class=\"next-disabled\" href=\"javascript:goPageList(" + (IndexPage + 1) + ",this);\">下一页</a>";
            }

            if (showbutton)
            {
                PageListTxt += "<span class=\"current_f\"> 跳转第 <input type=\"text\" name=\"toPageNo\" id=\"pagerToPage2\" class=\"page_num\" maxlength=\"3\" size=\"1\">页";
                PageListTxt += "<input type=\"button\" class=\"page_btn page_btn_ov\" onclick=\"goToPageList()\" value=\"确定\"></span>";
                PageListTxt += "</p></div>";
            }
            return PageListTxt;
        }
    }
}